//
//  XCHTTPManager.m
//  Project
//
//  Created by liu guoqiang on 16/7/8.
//
//

#import "XCHTTPManager.h"


@implementation XCHTTPManager{
    AFHTTPRequestOperationManager *_manager;
}

static XCHTTPManager *shareManager = nil;

+(instancetype)shareManager{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareManager = [[self alloc]init];
    });
    return shareManager;
}

-(instancetype)init{
    self = [super init];
    if (self) {
        _manager = [AFHTTPRequestOperationManager manager];
        _manager.requestSerializer.timeoutInterval = 10;
    }
    return self;
}

-(void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    _manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [_manager GET:url parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
    
}

-(void)post:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failured:(void (^)(NSError *))failure{
    _manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [_manager POST:url parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}
@end
